package main

import (
	"fmt"
	"strconv"
)

type Element interface{}

type IntrefaceTest interface {
	test()
}

type List []Element

type Person struct {
	name string
	age  int
}

//下面这个是接口测试的，我们定义的类型有没有实现指定的接口的
//var _ IntrefaceTest = new(Person)
var _ IntrefaceTest = (*Person)(nil)

//*T类型的实现接口和T类型的实现接口是不同的
//func (this *Person) test() {
//	fmt.Println("interface test")
//}

//类型T实现了接口，则类型T的指针*T也实现了接口？
func (this Person) test() {
	fmt.Println("interface test")
}

//打印
func (p Person) String() string {
	return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}

func main() {
	list := make(List, 3)
	list[0] = 1       //an int
	list[1] = "Hello" //a string
	list[2] = Person{"Dennis", 70}

	//类型断言，主要是接口测试的
	for index, element := range list {

		//有没有实现接口
		if value, ok := element.(Element); ok {
			fmt.Printf("list[%d] is an Element and its value is %v\n", index, value)
		}

		switch value := element.(type) {
		case int:
			fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
		case string:
			fmt.Printf("list[%d] is a string and its value is %s\n", index, value)
		case Person:
			fmt.Printf("list[%d] is a Person and its value is %s\n", index, value)
		default:
			fmt.Println("list[%d] is of a different type", index)
		}
	}
}
